<template>
  <view class="user-page">
    <!-- 页面标题 -->
    <view class="page-header">
      <text class="page-title">个人中心</text>
    </view>
    
    <!-- 用户信息卡片 -->
    <view class="user-card">
      <view class="avatar-container">
        <view class="avatar">{{ userInfo.avatar || '👤' }}</view>
        <view class="avatar-shadow"></view>
      </view>
      <view class="user-info">
        <text class="nickname">{{ userInfo.nickname }}</text>
        <text class="user-id">ID: {{ userInfo.userId }}</text>
      </view>
      <button class="edit-btn" @click="editUserInfo">
        <text class="edit-icon">✏️</text>
        编辑
      </button>
    </view>

    <!-- 功能列表 -->
    <view class="function-section">
      <text class="section-title">密友功能</text>
      <view class="function-list">
        <!-- 密友管理 -->
        <navigator url="/pages/user/friends" class="function-item">
          <view class="item-content">
            <text class="item-icon">👥</text>
            <text class="item-text">密友管理</text>
          </view>
          <view class="item-extra">
            <text class="arrow">→</text>
          </view>
        </navigator>
        
        <!-- 查看权限设置 -->
        <view class="function-item">
          <view class="item-content">
            <text class="item-icon">🔒</text>
            <text class="item-text">心情统计查看权限</text>
          </view>
          <view class="item-extra">
            <switch 
              :checked="permissions.enabled" 
              @change="togglePermissions"
              class="permission-switch"
            ></switch>
            <text class="permission-text">{{ permissions.enabled ? '已开启' : '已关闭' }}</text>
          </view>
        </view>
        
        <!-- 好友心情统计 -->
        <navigator url="/pages/user/friendStats" class="function-item">
          <view class="item-content">
            <text class="item-icon">📊</text>
            <text class="item-text">好友心情统计</text>
          </view>
          <view class="item-extra">
            <text class="arrow">→</text>
          </view>
        </navigator>
        
        <!-- 消息中心 -->
        <navigator url="/pages/user/messages" class="function-item">
          <view class="item-content">
            <text class="item-icon">💬</text>
            <text class="item-text">消息中心</text>
          </view>
          <view class="item-extra">
            <view v-if="unreadCount > 0" class="unread-badge">{{ unreadCount }}</view>
            <text class="arrow">→</text>
          </view>
        </navigator>
      </view>
    </view>
    
    <!-- 关于 -->
    <view class="about-section">
      <text class="about-text">轻记 v1.0.0</text>
      <text class="about-desc">专注于记录生活的美好瞬间</text>
    </view>
  </view>
</template>

<script>
import { getUserInfo, saveUserInfo, getViewPermissions, saveViewPermissions, getFriendMessages } from '@/utils/storage'

export default {
  data() {
    return {
      userInfo: {},
      permissions: {
        enabled: true,
        friends: []
      }
    }
  },
  computed: {
    unreadCount() {
      const messages = getFriendMessages()
      return messages.filter(msg => !msg.read).length
    }
  },
  onLoad() {
    this.loadUserInfo()
    this.loadPermissions()
  },
  methods: {
    loadUserInfo() {
      this.userInfo = getUserInfo()
    },
    
    loadPermissions() {
      this.permissions = getViewPermissions()
    },
    
    editUserInfo() {
      uni.showModal({
        title: '修改昵称',
        editable: true,
        placeholderText: '输入新昵称',
        defaultValue: this.userInfo.nickname,
        success: (res) => {
          if (res.confirm && res.content && res.content.trim()) {
            this.userInfo.nickname = res.content.trim()
            saveUserInfo(this.userInfo)
            uni.showToast({ title: '修改成功' })
          }
        }
      })
    },
    
    togglePermissions(e) {
      const enabled = e.detail ? e.detail.value : !this.permissions.enabled
      this.permissions.enabled = enabled
      saveViewPermissions(this.permissions)
      uni.showToast({
        title: enabled ? '已开启查看权限' : '已关闭查看权限',
        icon: 'none'
      })
    }
  }
}
</script>

<style scoped>
.user-page {
  padding: 20rpx;
  min-height: 100vh;
  background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
}

/* 页面标题 */
.page-header {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10rpx);
  text-align: center;
}

.page-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

/* 用户信息卡片 */
.user-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 25rpx;
  padding: 40rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30rpx;
  box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10rpx);
  transition: all 0.3s ease;
}

.user-card:active {
  transform: scale(0.98);
}

.avatar-container {
  position: relative;
  margin-bottom: 25rpx;
}

.avatar {
  width: 140rpx;
  height: 140rpx;
  border-radius: 50%;
  background: linear-gradient(135deg, #2196f3, #1976d2);
  color: white;
  font-size: 70rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 20rpx rgba(33, 150, 243, 0.4);
  position: relative;
  z-index: 2;
  transition: all 0.3s ease;
}

.avatar:active {
  transform: scale(1.1);
}

.avatar-shadow {
  position: absolute;
  top: 10rpx;
  left: 10rpx;
  right: -10rpx;
  bottom: -10rpx;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.1);
  z-index: 1;
}

.user-info {
  text-align: center;
  margin-bottom: 25rpx;
}

.nickname {
  font-size: 40rpx;
  font-weight: bold;
  display: block;
  margin-bottom: 15rpx;
  color: #333;
}

.user-id {
  font-size: 26rpx;
  color: #666;
  display: block;
  background: rgba(33, 150, 243, 0.1);
  padding: 8rpx 15rpx;
  border-radius: 15rpx;
  display: inline-block;
}

.edit-btn {
  background: linear-gradient(135deg, #2196f3, #1976d2);
  color: white;
  border-radius: 50rpx;
  padding: 15rpx 40rpx;
  font-size: 26rpx;
  border: none;
  outline: none;
  box-shadow: 0 4rpx 15rpx rgba(33, 150, 243, 0.3);
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 10rpx;
}

.edit-btn:active {
  transform: scale(0.95);
  box-shadow: 0 2rpx 10rpx rgba(33, 150, 243, 0.2);
}

.edit-icon {
  font-size: 28rpx;
}

/* 功能列表 */
.function-section {
  margin-bottom: 30rpx;
}

.section-title {
  font-size: 24rpx;
  font-weight: bold;
  color: rgba(0, 0, 0, 0.6);
  margin-bottom: 15rpx;
  display: block;
  padding: 0 10rpx;
}

.function-list {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 25rpx;
  overflow: hidden;
  box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10rpx);
}

.function-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 35rpx;
  border-bottom: 1rpx solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.function-item:active {
  background: rgba(102, 126, 234, 0.1);
}

.function-item:last-child {
  border-bottom: none;
}

.item-content {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.item-icon {
  font-size: 36rpx;
  width: 40rpx;
  text-align: center;
}

.item-text {
  font-size: 28rpx;
  color: #333;
}

.item-extra {
  display: flex;
  align-items: center;
  gap: 15rpx;
}

.arrow {
  color: #999;
  font-size: 36rpx;
  font-weight: bold;
}

.unread-badge {
  background: linear-gradient(135deg, #ff6b6b, #ee5a6f);
  color: white;
  border-radius: 20rpx;
  padding: 5rpx 20rpx;
  font-size: 24rpx;
  font-weight: bold;
  box-shadow: 0 2rpx 10rpx rgba(255, 107, 107, 0.3);
}

/* 权限设置 */
.permission-switch {
  transform: scale(1.2);
}

.permission-text {
  font-size: 24rpx;
  color: #666;
}

/* 关于 */
.about-section {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 25rpx;
  padding: 30rpx;
  box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10rpx);
  text-align: center;
}

.about-text {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 10rpx;
}

.about-desc {
  font-size: 24rpx;
  color: #666;
}
</style>